/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.app.ad.service.impl;

import me.zhengjie.modules.app.ad.domain.AduserXTabsindex;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.app.ad.repository.AduserXTabsindexRepository;
import me.zhengjie.modules.app.ad.service.AduserXTabsindexService;
import me.zhengjie.modules.app.ad.service.dto.AduserXTabsindexDto;
import me.zhengjie.modules.app.ad.service.dto.AduserXTabsindexQueryCriteria;
import me.zhengjie.modules.app.ad.service.mapstruct.AduserXTabsindexMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author HaojieLi
* @date 2021-03-20
**/
@Service
@RequiredArgsConstructor
public class AduserXTabsindexServiceImpl implements AduserXTabsindexService {

    private final AduserXTabsindexRepository aduserXTabsindexRepository;
    private final AduserXTabsindexMapper aduserXTabsindexMapper;

    @Override
    public Map<String,Object> queryAll(AduserXTabsindexQueryCriteria criteria, Pageable pageable){
        Page<AduserXTabsindex> page = aduserXTabsindexRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(aduserXTabsindexMapper::toDto));
    }

    @Override
    public List<AduserXTabsindexDto> queryAll(AduserXTabsindexQueryCriteria criteria){
        return aduserXTabsindexMapper.toDto(aduserXTabsindexRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public AduserXTabsindexDto findById(String id) {
        AduserXTabsindex aduserXTabsindex = aduserXTabsindexRepository.findById(id).orElseGet(AduserXTabsindex::new);
        ValidationUtil.isNull(aduserXTabsindex.getId(),"AduserXTabsindex","id",id);
        return aduserXTabsindexMapper.toDto(aduserXTabsindex);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AduserXTabsindexDto create(AduserXTabsindex resources) {
        resources.setId(IdUtil.simpleUUID()); 
        return aduserXTabsindexMapper.toDto(aduserXTabsindexRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(AduserXTabsindex resources) {
        AduserXTabsindex aduserXTabsindex = aduserXTabsindexRepository.findById(resources.getId()).orElseGet(AduserXTabsindex::new);
        ValidationUtil.isNull( aduserXTabsindex.getId(),"AduserXTabsindex","id",resources.getId());
        aduserXTabsindex.copy(resources);
        aduserXTabsindexRepository.save(aduserXTabsindex);
    }

    @Override
    public void deleteAll(String[] ids) {
        for (String id : ids) {
            aduserXTabsindexRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<AduserXTabsindexDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (AduserXTabsindexDto aduserXTabsindex : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put(" userId",  aduserXTabsindex.getUserId());
            map.put(" tabsindex",  aduserXTabsindex.getTabsindex());
            map.put(" createTime",  aduserXTabsindex.getCreateTime());
            map.put(" updateTime",  aduserXTabsindex.getUpdateTime());
            map.put(" isEnable",  aduserXTabsindex.getIsEnable());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}